package pers.jphoebe.quartz.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import pers.jphoebe.quartz.entity.juhe.GoldData;
import pers.jphoebe.quartz.entity.juhe.GoldDataMap;
import pers.jphoebe.quartz.entity.juhe.JuheResponse;
import pers.jphoebe.quartz.entity.response.ErrorCode;
import pers.jphoebe.quartz.entity.response.ResponseData;
/**
 * Created by 蒋时华 on 2017/10/08.
 */
public class FastJsonTools {

	public FastJsonTools() {
		// TODO Auto-generated constructor stub
	}

	public static String createJsonString(Object object) {
		String jsonString = JSON.toJSONString(object);
		return jsonString;
	}

	public static <T> T toBean(String jsonString, Class<T> cls) {
		T t = JSON.parseObject(jsonString, cls);
		return t;
	}

	public static <T> ResponseData<T> toBeanForResponsedata(String jsonString, Class<T> cls) {
		ResponseData responseData = null;
		responseData = JSON.parseObject(jsonString, ResponseData.class);
//		if(ErrorCode.SUCCESS_CODE.getCode() == responseData.getCode()){
		if(responseData.getData()!=null){
			T t = JSON.parseObject(responseData.getData().toString(), cls);
			responseData.setData(t);
			responseData.setErrorCode(ErrorCode.SUCCESS_CODE);
		}
		return (ResponseData<T>)responseData;
	}

	public static JuheResponse<ArrayList<GoldDataMap>> toBeanForJuheResponsedataListGoldDataList(String jsonString) {
		JuheResponse<ArrayList<GoldDataMap>> juheResponse = null;
		juheResponse = JSON.parseObject(jsonString, JuheResponse.class);
		if(juheResponse.getResult()!=null){
			ArrayList<GoldDataMap> goldDataLists = new ArrayList<>();
			List<String> strings = FastJsonTools.toBeanForList(FastJsonTools.createJsonString(juheResponse.getResult()), String.class);
			if(strings!=null && strings.get(0)!=null){
				GoldDataMap tempMap = FastJsonTools.toBean(strings.get(0), GoldDataMap.class);
				GoldDataMap goldDataMap = new GoldDataMap();
				Set<String> keySet = tempMap.keySet();
				for(String key:keySet){
					String jsonString1 = FastJsonTools.createJsonString(tempMap.get(key));
					GoldData goldData = FastJsonTools.toBean(jsonString1, GoldData.class);
					goldDataMap.put(key,goldData);
				}
				goldDataLists.add(goldDataMap);
			}
			juheResponse.setResult(goldDataLists);
		}
		return juheResponse;
	}

	public static <T> List<T> toBeanForList(String jsonString,Class<T> cls) {
		List<T> list = null;
		list = JSON.parseArray(jsonString, cls);
		return list;
	}

	/**
	 * @param jsonString
	 * @return
	 */
	public static List<Map<String, Object>> toBeanForListMap(
			String jsonString) {
		List<Map<String, Object>> list = JSON.parseObject(jsonString,
				new TypeReference<List<Map<String, Object>>>() {
				});
		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {


	}

}
